### Dan Hopkins
### 5/3/2018

library(foreign)
library(xtable)

the_prefix <- ""

dta14 <- read.spss(
    paste0(
        the_prefix,
        "kffnongroup/Wave 1 2014/N1008_2.sav"),
    to.data.frame=T
)

dta14$ACAFAVOR <- NA
dta14$ACAFAVOR[dta14$qn2=="Very favorable        "] <- 4
dta14$ACAFAVOR[dta14$qn2=="Somewhat favorable    "] <- 3
dta14$ACAFAVOR[dta14$qn2=="Somewhat unfavorable  "] <- 2
dta14$ACAFAVOR[dta14$qn2=="Very unfavorable      "] <- 1

### variable indicating those who purchased via exchanges directly
dta14$BUYEXCHANGE <- 0
dta14$BUYEXCHANGE[dta14$qn35=="From healthcare.gov or (state specific marketplace name)"] <- 1

### variable indicating those who have plans from exchange
dta14$EXCHANGEPLAN <- 0
dta14$EXCHANGEPLAN[dta14$qn35=="From healthcare.gov or (state specific marketplace name)" | dta14$qn36=="Plan purchased from a state or federal marketplace  "] <- 1

dta14$FEDPOVPCT <- NA
dta14$FEDPOVPCT[dta14$fplincom=="<138%"] <- 100
dta14$FEDPOVPCT[dta14$fplincom=="138-250%"] <- 200
dta14$FEDPOVPCT[dta14$fplincom=="250-400%"] <- 325
dta14$FEDPOVPCT[dta14$fplincom=="138-400% unspec"] <- 269
dta14$FEDPOVPCT[dta14$fplincom=="400%+"] <- 600

dta14$REGISTERED <- 1*(dta14$qnd13=="Yes  ")

dta14$FEDWEBSITE <- 0
dta14$FEDWEBSITE[dta14$BUYEXCHANGE==1 & dta14$stwebenr=="Healthcare.gov"] <- 1

dta14$SUBSIDY <- NA
dta14$SUBSIDY[dta14$qn23=="No, not getting financial assistance  "] <- 0
dta14$SUBSIDY[dta14$qn23=="Yes, getting financial assistance     "] <- 1

dta14$PRIORUNINSURED <- 0
dta14$PRIORUNINSURED[dta14$previnst=="Uninsured"] <- 1

dta14$MALE <- 1*(dta14$qns14=="Male    ")

dta14$AGE <- as.numeric(as.character(dta14$ageques1))
dta14$AGE[dta14$ageques2=="30-39"] <- 35
dta14$AGE[dta14$ageques2=="40-49"] <- 45
dta14$AGE[dta14$ageques2=="50-64"] <- 57

dta14$HEALTH <- NA
dta14$HEALTH[dta14$qnd1=="Excellent    "] <- 5
dta14$HEALTH[dta14$qnd1=="Very good    "] <- 4
dta14$HEALTH[dta14$qnd1=="Good         "] <- 3
dta14$HEALTH[dta14$qnd1=="Fair         "] <- 2
dta14$HEALTH[dta14$qnd1=="Poor         "] <- 1

dta14$PREEXIST <- NA
dta14$PREEXIST[dta14$qnd3=="Yes, someone has pre-existing condition "] <- 1
dta14$PREEXIST[dta14$qnd3=="No, no one has pre-existing condition   "] <- 0

dta14$PID5 <- NA
dta14$PID5[dta14$affil_1 %in% c("Other affiliation","An Independent","None/no affiliation","Don't know")] <- 3
dta14$PID5[dta14$affil_1=="A Democrat"] <- 1
dta14$PID5[dta14$affil_1 %in% c("Other affiliation","An Independent","None/no affiliation","Don't know","Refused") & dta14$qnd10=="Democratic                                             "] <- 2
dta14$PID5[dta14$affil_1 %in% c("Other affiliation","An Independent","None/no affiliation","Don't know","Refused") & dta14$qnd10=="Republican                                             "] <- 4
dta14$PID5[dta14$affil_1=="A Republican"] <- 5

dta14$ZIP <- as.numeric(as.character(dta14$qnzip))

dta14$EDUC <- NA
dta14$EDUC[dta14$educ=="Less than high school graduate"] <- 10
dta14$EDUC[dta14$educ=="High school graduate"] <- 12
dta14$EDUC[dta14$educ=="Technical school/other"] <- 13
dta14$EDUC[dta14$educ=="Some college (including Associate's degree)"] <- 14
dta14$EDUC[dta14$educ=="Graduated college (4 year/Bachelor's degree)"] <- 16
dta14$EDUC[dta14$educ=="Graduate school or more"] <- 19

dta14$HISPANIC <- 1*(dta14$hispanc1=="Yes")
dta14$BLACK <- 1*(dta14$newrace=="Black non-Hispanc")
dta14$ASIAN <- 1*(dta14$newrace=="Asian")

dta14$CELL <- 1*(dta14$samptype=="Cell")

## prop.table(table(dta14$qn60[dta14$BUYEXCHANGE==1]))
## table(dta14$qn60[dta14$BUYEXCHANGE==0])


print(
xtable(
    cbind(
        `Non-group`=prop.table(table(dta14$qn60)),
        `Non-group: exchange only`=prop.table(table(dta14$qn60[dta14$BUYEXCHANGE==1]))
    ),
    caption="This table shows the prior insurance sources for respondents in the KFF 2014 non-group survey.",
    label="t:priorinsurance"
),
file=paste0(the_prefix, "figs/tableA5_prior_insurance_sources.tex")
)


### samptype, strata
lout14 <- lm(
    ACAFAVOR ~ AGE+as.factor(EDUC)+I(FEDPOVPCT/100)+MALE+HISPANIC+BLACK+ASIAN+as.factor(PID5)+EXCHANGEPLAN+BUYEXCHANGE++SUBSIDY+familysize+as.factor(strata)+CELL,
    weights=weight,
    data=dta14
)

#############
dta15 <- read.spss(
    paste0(
        the_prefix, "kffnongroup/Wave 2 2015/n1124.sav"),
   to.data.frame=T
)

dta15$BLACK <- 1*(dta15$newrace=="Black non-Hispanc")
dta15$ASIAN <- 1*(dta15$newrace=="Asian")
dta15$HISPANIC <- 1*(dta15$newrace=="Hispanic")

dta15$ZIP <- as.numeric(as.character(dta15$qnzip))

dta15$STCOFIPS <- as.numeric(as.character(dta15$censfips))

dta15$EDUC <- NA
dta15$EDUC[dta15$educ=="Less than high school graduate"] <- 10
dta15$EDUC[dta15$educ=="High school graduate"] <- 12
dta15$EDUC[dta15$educ=="Technical school/other"] <- 13
dta15$EDUC[dta15$educ=="Some college (including Associate's degree)"] <- 14
dta15$EDUC[dta15$educ=="Graduated college (4 year/Bachelor's degree)"] <- 16
dta15$EDUC[dta15$educ=="Graduate school or more"] <- 19

dta15$ACAFAVOR <- NA
dta15$ACAFAVOR[dta15$ng1=="Very favorable        "] <- 4
dta15$ACAFAVOR[dta15$ng1=="Somewhat favorable    "] <- 3
dta15$ACAFAVOR[dta15$ng1=="Somewhat unfavorable  "] <- 2
dta15$ACAFAVOR[dta15$ng1=="Very unfavorable      "] <- 1

### variable indicating those who purchased via exchanges directly
dta15$BUYEXCHANGE <- 0
dta15$BUYEXCHANGE[dta15$ng19=="From healthcare.gov (or INSERT STATE SPECIFIC MARKETPLACE NAME) "] <- 1

### variable indicating those who have plans from exchange
dta15$EXCHANGEPLAN <- 0
dta15$EXCHANGEPLAN[dta15$ng19=="From healthcare.gov (or INSERT STATE SPECIFIC MARKETPLACE NAME) " | dta15$ng20=="Marketplace plan"] <- 1

dta15$FEDPOVPCT <- NA
dta15$FEDPOVPCT[dta15$fplincom=="<138%"] <- 100
dta15$FEDPOVPCT[dta15$fplincom=="138-250%"] <- 200
dta15$FEDPOVPCT[dta15$fplincom=="250-400%"] <- 325
dta15$FEDPOVPCT[dta15$fplincom=="138-400% unspec"] <- 269
dta15$FEDPOVPCT[dta15$fplincom=="400%+"] <- 600

dta15$SUBSIDY <- 0
dta15$SUBSIDY[dta15$ng48=="No, not getting financial assistance  "] <- 0
dta15$SUBSIDY[dta15$ng48=="Yes, getting financial assistance     "] <- 1

dta15$MALE <- 1*(dta15$qns14=="Male")

dta15$AGE <- as.numeric(as.character(dta15$ageques1))
dta15$AGE[dta15$ageques2=="30-39"] <- 35
dta15$AGE[dta15$ageques2=="40-49"] <- 45
dta15$AGE[dta15$ageques2=="50-64"] <- 57

dta15$HEALTH <- NA
dta15$HEALTH[dta15$qnd1=="Excellent    "] <- 5
dta15$HEALTH[dta15$qnd1=="Very good    "] <- 4
dta15$HEALTH[dta15$qnd1=="Good         "] <- 3
dta15$HEALTH[dta15$qnd1=="Fair         "] <- 2
dta15$HEALTH[dta15$qnd1=="Poor         "] <- 1

dta15$PID5 <- NA
dta15$PID5[dta15$affil_1 %in% c("Other affiliation","An Independent","None/no affiliation","Don't know")] <- 3
dta15$PID5[dta15$affil_1=="A Democrat, or"] <- 1
dta15$PID5[dta15$affil_1 %in% c("Other affiliation","An Independent","None/no affiliation","Don't know","Refused") & dta15$qnd10=="Democratic                                             "] <- 2
dta15$PID5[dta15$affil_1 %in% c("Other affiliation","An Independent","None/no affiliation","Don't know","Refused") & dta15$qnd10=="Republican                                             "] <- 4
dta15$PID5[dta15$affil_1=="A Republican"] <- 5

dta15$CELL <- 1*(dta15$samptype=="CELL")

lout15 <- lm(
    ACAFAVOR ~ as.factor(EDUC)+MALE+HISPANIC+BLACK+ASIAN+as.factor(PID5)+EXCHANGEPLAN+BUYEXCHANGE+SUBSIDY+CELL+familysize++I(FEDPOVPCT/100)+AGE,
    data=dta15,
    weights=newwght
)



##########

dta16 <- read.spss(
    paste0(
        the_prefix, "kffnongroup/Wave 3 2016/o1103_client 040816.sav"),
    to.data.frame=T
)

dta16$ACAFAVOR <- NA
dta16$ACAFAVOR[dta16$ng2=="Very favorable        "] <- 4
dta16$ACAFAVOR[dta16$ng2=="Somewhat favorable    "] <- 3
dta16$ACAFAVOR[dta16$ng2=="Somewhat unfavorable  "] <- 2
dta16$ACAFAVOR[dta16$ng2=="Very unfavorable      "] <- 1

dta16$PID5 <- 3
dta16$PID5[dta16$leaned=="Democrat"] <- 1
dta16$PID5[dta16$leaned=="Lean Democrat"] <- 2
dta16$PID5[dta16$leaned %in% c("Don't know","Independent/don't lean","(Other)")] <- 3
dta16$PID5[dta16$leaned=="Lean Republican"] <- 4
dta16$PID5[dta16$leaned=="Republican"] <- 5

dta16$BUYEXCHANGE <- 0
dta16$BUYEXCHANGE[dta16$ng16=="From healthcare.gov or state marketplace"] <- 1

dta16$EXCHANGEPLAN <- 0
dta16$EXCHANGEPLAN[dta16$ng16=="From healthcare.gov or state marketplace" | dta16$ng17=="Marketplace plan"] <- 1

dta16$LIKELYEXCHANGE <- 0
dta16$LIKELYEXCHANGE[dta16$exchange=="Likely exchange"] <- 1

dta16$SUBSIDY <- NA
dta16$SUBSIDY[dta16$ng18=="No, not getting financial assistance  "] <- 0
dta16$SUBSIDY[dta16$ng18=="Yes, getting financial assistance     "] <- 1

dta16$MALE <- 1*(dta16$qns14=="Male")

dta16$AGE <- as.numeric(as.character(dta16$ageques1))
dta16$AGE[dta16$ageques2=="30-39"] <- 35
dta16$AGE[dta16$ageques2=="40-49"] <- 45
dta16$AGE[dta16$ageques2=="50-64"] <- 57

dta16$HEALTH <- NA
dta16$HEALTH[dta16$qnd1=="Excellent    "] <- 5
dta16$HEALTH[dta16$qnd1=="Very good    "] <- 4
dta16$HEALTH[dta16$qnd1=="Good         "] <- 3
dta16$HEALTH[dta16$qnd1=="Fair         "] <- 2
dta16$HEALTH[dta16$qnd1=="Poor         "] <- 1

dta16$BLACK <- 1*(dta16$race=="Black or African American")
dta16$ASIAN <- 1*(dta16$race=="Asian/Chinese/Japanese")
dta16$HISPANIC <- 1*(dta16$race=="Hispanic/Latino")

dta16$ZIP <- as.numeric(as.character(dta16$qnzip))

dta16$EDUC <- NA
dta16$EDUC[dta16$educ=="Less than high school graduate"] <- 10
dta16$EDUC[dta16$educ=="High school graduate"] <- 12
dta16$EDUC[dta16$educ=="Technical school/other"] <- 13
dta16$EDUC[dta16$educ=="Some college (including Associate's degree)"] <- 14
dta16$EDUC[dta16$educ=="Graduated college (4 year/Bachelor's degree)"] <- 16
dta16$EDUC[dta16$educ=="Graduate school or more"] <- 19

dta16$CELL <- 1*(dta16$cell2=="Continue, on cell phone and not driving" & ! dta16$cell2 %in% c(NA))

dta16$FEDPOVPCT <- NA
dta16$FEDPOVPCT[dta16$incsum=="Less than amount 1"] <- 100
dta16$FEDPOVPCT[dta16$incsum=="Less than amount 2"] <- 200
dta16$FEDPOVPCT[dta16$incsum=="Amount 2 or more"] <- 325
dta16$FEDPOVPCT[dta16$incsum=="More than amount 1 (unspecified)"] <- 269
dta16$FEDPOVPCT[dta16$incsum=="Amount 3 or more"] <- 600



####
lout16 <- lm(
    ACAFAVOR ~ as.factor(EDUC)++MALE+HISPANIC+BLACK+ASIAN+as.factor(PID5)+EXCHANGEPLAN+BUYEXCHANGE+SUBSIDY+CELL+familysize++I(FEDPOVPCT/100)+AGE,
    data=dta16,
    weights=weight
)


stargazer(
    lout14,lout15,lout16,digits=2,
    ## custom.model.names=c("2014","2015","2016"),
    ## omit=paste(c(
    ##     "AGE","EDUC","MALE","HISPANIC","BLACK","ASIAN","PID",
    ##     "familysize","strata","FED","CELL","Constant"
    ## ), collapse="|"),
    ## covariate.labels=c(
    ##     "\\multirow{3}{4cm}{Health Plan Meets Exchange Regulations}","Used Exchange","Insurance Subsidized"
    ## ),
    omit.stat=c("ser","f","adj.rsq", "rsq"),
    table.layout = "ts",
    float=F,
    star.cutoffs = c(0.05, 0.01, 0.001),
    out=paste0(the_prefix, "figs/tableA6_aca_favorability_nongroup.tex")
)
